{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 日期：2022-10-19（第九周）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周学习目标  \n",
    "> 1.列表（list）的方法  \n",
    "* 使用.Tab键查看列表的方法  \n",
    "* 使用help(查看方法的使用规则)  \n",
    "\n",
    "> 2.while循环  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小黄']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names=[\"小白\",\"小红\",\"小黄\"]\n",
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.1 列表的增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小黄', '小蓝']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.append(\"小蓝\")\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.append) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.insert)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小黄']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names=[\"小白\",\"小红\",\"小黄\"]\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "names.insert(2,\"小黑\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小黑', '小黄']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.列表的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小黄', '小蓝', '小黑']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names=[\"小白\",\"小红\",\"小黄\",\"小蓝\",\"小黑\"]\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "names.remove(\"小黄\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小蓝', '小黑']"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 正确的删除方法的流程  \n",
    "> 1.查询value是否存在列表当中  \n",
    "> 2.对存在的value进行删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"Wang\" in names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "list.remove(x): x not in list",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-5-87f5ed08eeb1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnames\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Wang\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: list.remove(x): x not in list"
     ]
    }
   ],
   "source": [
    "names.remove(\"Wang\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"wang\" in names:\n",
    "    name.remove(\"wang\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实践：删除系统：<br>1.如果输入的值存在，删除；<br>2.如果输入的结果不存在，提示用户查询无果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小蓝', '小黑']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入要删除的内容：小红\n",
      "删除成功！\n",
      "['小白', '小蓝', '小黑']\n"
     ]
    }
   ],
   "source": [
    "user_input_name = input(\"请输入要删除的内容：\")\n",
    "if user_input_name in names:\n",
    "    names.remove(user_input_name)\n",
    "    print(\"删除成功！\")\n",
    "else:\n",
    "    print(\"您输入的结果不存在，删除无效\")\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.列表的删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小黄', '小蓝', '小黑']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names=[\"小白\",\"小红\",\"小黄\",\"小蓝\",\"小黑\"]\n",
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.remove)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小黑'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop() # 直接使用pop(),删除最后一位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小黄', '小蓝']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names=[\"小白\",\"小红\",\"小黄\"]\n",
    "len(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'小黄'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names.pop(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小黄']"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "names=[\"小白\",\"小红\",\"小黄\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入删除的索引值：2\n"
     ]
    }
   ],
   "source": [
    "pop_index = int(input(\"输入删除的索引值：\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "删除成功！\n"
     ]
    }
   ],
   "source": [
    "if pop_index<len(names):\n",
    "    names.pop(pop_index)\n",
    "    print(\"删除成功！\")\n",
    "else:\n",
    "    print(\"您输入的值不在index的范围内\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.列表的修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "names=[\"小白\",\"小红\",\"小黄\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "names[0] = \"小黑\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小黑', '小红', '小黄']"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.列表的其他方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "names=[\"小白\",\"小红\",\"小黄\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "names.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "names_class1 =[\"小白\",\"小红\",\"小黄\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小黄']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(iterable, /) method of builtins.list instance\n",
      "    Extend list by appending elements from the iterable.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.extend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小黄', '小红']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class2=[\"小白\",\"小黄\",\"小红\"]\n",
    "names_class2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "names_class1.extend(names_class2) # 注意：使用列表的方法的列表进行扩展"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['小白', '小红', '小黄', '小白', '小黄', '小红']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names_class1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* sort()排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生 =[230,250,245,255,260,270]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function sort:\n",
      "\n",
      "sort(*, key=None, reverse=False) method of builtins.list instance\n",
      "    Stable sort *IN PLACE*.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(立定跳远_男生.sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "立定跳远_男生.sort(reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[270, 260, 255, 250, 245, 230]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "立定跳远_男生"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.while循环  \n",
    "\n",
    "*while True 是循环概念中非常重要的一种方式。它可以实现for循环的指定次数，也可以在特定条件下停止。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙，缠缠绵绵到天涯.. 0\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 1\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 2\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 3\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 4\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 5\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 6\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 7\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 8\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 9\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 10\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 11\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 12\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 13\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 14\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 15\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 16\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 17\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 18\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 19\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 20\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 21\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 22\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 23\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 24\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 25\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 26\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 27\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 28\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 29\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 30\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 31\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 32\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 33\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 34\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 35\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 36\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 37\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 38\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 39\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 40\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 41\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 42\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 43\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 44\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 45\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 46\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 47\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 48\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 49\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 50\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 51\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 52\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 53\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 54\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 55\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 56\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 57\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 58\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 59\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 60\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 61\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 62\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 63\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 64\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 65\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 66\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 67\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 68\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 69\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 70\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 71\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 72\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 73\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 74\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 75\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 76\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 77\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 78\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 79\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 80\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 81\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 82\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 83\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 84\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 85\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 86\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 87\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 88\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 89\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 90\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 91\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 92\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 93\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 94\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 95\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 96\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 97\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 98\n",
      "你是风儿我是沙，缠缠绵绵到天涯.. 99\n"
     ]
    }
   ],
   "source": [
    "# 死循环\n",
    "count=0\n",
    "while True:\n",
    "    print(\"你是风儿我是沙，缠缠绵绵到天涯..\",count)\n",
    "    count =count+1\n",
    "    if count==100:\n",
    "        break"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 猜数字\n",
    "### 1.hide_card :0-100  \n",
    "### 2.guess_digit:50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. hide_card : 0-100,大了，小了\n",
    "# 2. guess_digit : 50\n",
    "count = 0\n",
    "hide_card = int(input(\"hide_card:\"))\n",
    "\n",
    "while True:\n",
    "    guess_digit = int(input(\"猜一猜（0-100）：\"))\n",
    "    # 对比:\n",
    "    if hide_card == guess_digit:\n",
    "        print(\"猜对了！真棒！\")\n",
    "        break\n",
    "    elif guess_digit>hide_card:\n",
    "        print(\"往小猜！再试一次！\")\n",
    "    else:\n",
    "        print(\"往大的猜！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课后作业  \n",
    "> 1.列表的切片和方法的使用  \n",
    ">> a.课本p67页  \n",
    ">> b.课本p81页  \n",
    ">> c.课本p89页 列表，我们已知道什么（总结）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本p67页"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"Don't panic!\" # 取字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "plist = list(phrase) # 将字符串转换为列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(phrase)  # 输出字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "print(plist) # 输出转换为列表后的字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 要输出\"on tap\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist.pop(0) # 删除D"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.remove(\"'\") # 删除单引号，此时输出为：ont panic!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(4): # range(4)是循环四次，即删除\"nic!\"，此时输出为：ont pa\n",
    "    plist.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 此时需要将空格\" \"与\"t\"交换位置，\"p\"与\"a\"交换位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = ''.join(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on tap\n"
     ]
    }
   ],
   "source": [
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本p81页"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = ''.join(plist[1:3]) # 取出\"o\"、\"n\"\n",
    "new_phrase = new_phrase+''.join([plist[5],plist[4],plist[7],plist[6]]) #\"on\"+\"t\"+\"a\"+\"p\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on tap\n"
     ]
    }
   ],
   "source": [
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本p89页"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 可以用中括号[]从任何列表选择单个对象  \n",
    "* Python从0开始计数，所以列表中的第一个对象位于索引位置0，第2个对象处于索引位置1，以此类推。  \n",
    "* Python允许从任意两端索引列表。使用-1会选择列表中的最后一项，-2会选择倒数第2个对象。  \n",
    "* 列表支持在中括号记法中指定开始、结束和步长值，可以提供列表的切片（或片段）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
